package com.PMSys.Servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.PMSys.Demain.Post;
import com.PMSys.Demain.Staff;
import com.PMSys.Service.PostService;

public class PostServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public PostServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");

		doPost(request, response);
	}

	PostService postService = new PostService();

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");

		String addState = request.getParameter("addState");

		String PosterAll = request.getParameter("PosterAll");

		String selectState = request.getParameter("selectState");

		String editState = request.getParameter("editState");

		String updateState = request.getParameter("updateState");

		String deleteState = request.getParameter("deleteState");

		String selectByType = request.getParameter("selectByType");

		String selectByValue = request.getParameter("selectByValue");

		if (PosterAll != null && PosterAll != "") {
			doSelectAll(request, response);
		}

		if (addState != null && addState != "") {
			doAddPost(request, response);
		}

		if (selectState != null && selectState != "") {
			doSelectPart(request, response);
		}

		if (editState != null && editState != "") {
			doEditPart(request, response);
		}

		if (updateState != null && updateState != "") {
			doUpdatePart(request, response);
		}

		if (deleteState != null && deleteState != "") {
			doDeletePart(request, response);
		}

		if (selectByType != null && selectByType != "") {
			doSelectTypePart(request, response);
		}

		if (selectByValue != null && selectByValue != "") {
			doselectByValue(request, response);
		}

	}

	public void doSelectPart(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String selectCondition = request.getParameter("selectCondition");

		String selectConditionValue = request.getParameter("selectConditionValue");

		Post Poster = new Post();
		Post selectPost = new Post();
		List<Post> allPost = new ArrayList<Post>();

		if (selectConditionValue != null && selectConditionValue != "") {
			if (selectCondition.equals("1")) {

				selectPost.setP_id(selectConditionValue);
				Poster = postService.getPostbyId(selectPost);
				if (Poster != null) {
					allPost.add(Poster);
				}
			} else if (selectCondition.equals("2")) {

				selectPost.setP_name(selectConditionValue);
				allPost = postService.getPostbyName(selectPost);
			} else if (selectCondition.equals("3")) {

				selectPost.setP_type(selectConditionValue);
				allPost = postService.getPostbyType(selectPost);
			}

			else {

			}

			if (allPost.size() == 0) {
				response.getWriter().print("<script>alert('系统中不存在岗位，请先添加');location.href=" + "'"
						+ request.getContextPath() + "/servlet/PostServlet?PosterAll=1'</script>");
			} else {

				request.setAttribute("allPost", allPost);

				request.getRequestDispatcher("/Post/postMain.jsp").forward(request, response);
			}
		} else {
			response.getWriter().print("<script>alert('请输入查询条件');location.href=" + "'" + request.getContextPath()
					+ "/servlet/PostServlet?PosterAll=1'</script>");
		}

	}

	public void doSelectTypePart(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String selectType = request.getParameter("selectType");

		Post selectPost = new Post();
		List<Post> allPost = new ArrayList<Post>();

		selectPost.setP_type(selectType);
		allPost = postService.getPostbyType(selectPost);

		if (allPost.size() == 0) {
			response.getWriter().print("<script>alert('系统中不存在对应类型岗位，请先添加');location.href=" + "'"
					+ request.getContextPath() + "/servlet/PostServlet?PosterAll=1'</script>");
		} else {

			request.setAttribute("allPost", allPost);

			request.getRequestDispatcher("/Post/postMain.jsp").forward(request, response);
		}
	}

	public void doUpdatePart(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String id = request.getParameter("id");

		String p_name = request.getParameter("p_name");
		String p_type = request.getParameter("p_type");
		String p_compilation = request.getParameter("p_compilation");

		Post Poster = new Post();
		Poster.setP_id(id);
		Poster.setP_name(p_name);
		Poster.setP_type(p_type);
		Poster.setP_compilation(p_compilation);

		postService.updatePost(Poster);
		response.getWriter().print("<script>alert('修改成功');location.href=" + "'" + request.getContextPath()
				+ "/servlet/PostServlet?PosterAll=1'</script>");
	}

	public void doDeletePart(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String id = request.getParameter("id");

		Post Poster = new Post();
		Poster.setP_id(id);
		List<Staff> allstaff = new ArrayList<Staff>();
		allstaff = postService.getStaff(Poster);
		if (allstaff.size() != 0) {
			response.getWriter().print("<script>alert('岗位中存在员工，请先调整岗位');location.href=" + "'" + request.getContextPath()
					+ "/servlet/PostServlet?PosterAll=1'</script>");
		} else {
			postService.deletePost(id);

			response.getWriter().print("<script>alert('删除成功');location.href=" + "'" + request.getContextPath()
					+ "/servlet/PostServlet?PosterAll=1'</script>");
		}

	}

	public void doSelectAll(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		List<Post> allPost = postService.getAllPost();

		if (allPost.size() == 0) {
			response.getWriter().print("<script>alert('系统无岗位');location.href='" + request.getContextPath()
					+ "/Post/postMain.jsp'</script>");
		} else {

			request.setAttribute("allPost", allPost);
			System.out.print(allPost.size());

			request.getRequestDispatcher("/Post/postMain.jsp").forward(request, response);
		}
	}

	public void doEditPart(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String id = request.getParameter("id");

		Post selectPost = new Post();

		Post allPost = new Post();

		selectPost.setP_id(id);

		allPost = postService.getPostId(selectPost);

		request.setAttribute("allPost", allPost);

		request.getRequestDispatcher("/Post/updatePost.jsp").forward(request, response);
	}

	public void doAddPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String p_name = request.getParameter("p_name");
		String p_type = request.getParameter("p_type");
		String p_compilation = request.getParameter("p_compilation");

		Post poster = new Post();

		poster.setP_name(p_name);
		poster.setP_type(p_type);
		poster.setP_compilation(p_compilation);


		postService.addPost(poster);

		if (p_type.equals("1")) {
			poster.setP_type("管理");
		}
		if (p_type.equals("2")) {
			poster.setP_type("技术");
		}
		if (p_type.equals("3")) {
			poster.setP_type("营销");
		}
		if (p_type.equals("4")) {
			poster.setP_type("市场");
		}
		request.setAttribute("poster", poster);
		request.getRequestDispatcher("/Post/addResult.jsp").forward(request, response);
	}

	private void doselectByValue(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		Post post = new Post();
		List<Post> allPost = new ArrayList<Post>();

		String idString = request.getParameter("p_id");
		if (idString != null && !"".equals(idString)) {
			post.setP_id(idString);
		}

		String nameString = request.getParameter("p_name");
		if (nameString != null && !"".equals(nameString)) {
			post.setP_name(nameString);
		}

		String typeString = request.getParameter("p_type");
		if (typeString != null && !"".equals(typeString)) {
			post.setP_type(typeString);
		}

		allPost = postService.selectByValue(post);

		request.setAttribute("allPost", allPost);

		request.getRequestDispatcher("/Post/postMain.jsp").forward(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {

	}

}
